import { useCloud } from '@/modules/cloud/index';
const articleCol = useCloud().database().collection('article');
// const collectCol = useCloud().database().collection('collect');

export default {
    methods: {
        async getTargetArticle(docId = '') {
            const res = await articleCol.doc(docId).get()

            console.log('res', res, docId);

            let target = res.data[0]

            if (!target.views) {
                target = await this.initArticleData(docId)
            }


            this.videoData = target
            console.log('获取目标文章', target);
            return target
        },

        // 文章初始化数据库
        async initArticleData(docId = '') {
            await articleCol.doc(docId).update({
                likes: [],
                views: [],
                collects: [],
            })
            const res2 = await articleCol.doc(docId).get()
            const video = res2.data[0]

            console.log('该文章未初始化数据库', video);
            return video
        },

        // 更新视频浏览数
        async updateArticleViews(docId = '') {
            let video = await this.getTargetArticle(docId)


            const _id = video._id
            video.views.push({
                uid: this.$store.state.userInfo.uid || this.$reporter.getDeviceId(),
                deviceId: this.$reporter.getDeviceId(),
                viewTimeStamp: new Date().getTime()
            })

            const res2 = await articleCol.doc(_id).update({
                views: video.views
            })
            console.log('更新文章浏览数', res2, video);
        },

        async collectArticle() {
            const docId = this.article._id
            const reqData = {
                type: 'article',
                itemId: docId,
                item: this.article,
                path: this.$route.fullPath
            }
            const res = await this.$cloudApi.collectTarget({
                data: reqData
            })

            console.log('docid', reqData, this.article._id);


            if (res.message) {
                this.$showToast(res.message)
            } else {

                this.$showToast('收藏成功')
            }
            console.log('收藏成功', res);
            this.isCollect = true;
            return res

        },

        shareArticle() {
            const link = `${window.location.href}&wtagid=${6666}`;
            const content = `分享一个文章给你：${link}`;
            this.$showModal({
                title: "分享文章",
                content,
                confirmText: "复制到粘贴版",
                confirm: () => {
                    console.log("分享文章", link);
                    console.log("分享文章", link);
                }
            });
        },
    }
}